package cerebrum.controller.wallpaper;

import cerebrum.common.EmptyChecker;
import cerebrum.crawler.ImagesCrawler;
import cerebrum.enums.LogCodeEnum;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ImagesDownload {
    // 下载图片
    public static void downPhoto(String picAddress, String name, String dir) {
        HttpUtil.imgGet(picAddress, dir, name, true);
    }

    /**
     * 获取 pixabay.com的图片
     *
     * @return
     */
    private List<String> getPixabayImages() {
        String url = "https://pixabay.com/api/";
        url += "?" + "key=21245306-389ef877bb82191782fe7e97d&q=性感&lang=zh&min_width=1080&per_page=10";
        String str = HttpUtil.doGet(url, null);
        if (EmptyChecker.notEmpty(str)) {
            JSONObject parse = JSON.parseObject(str);
            JSONArray jsonArray = JSON.parseArray(String.valueOf(parse.get("hits")));
            List<String> images = new ArrayList<>();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject pa = JSON.parseObject(String.valueOf(jsonArray.get(i)));
                String largeImageURL = String.valueOf(pa.get("largeImageURL"));
                images.add(largeImageURL);
            }
            return images;
        }
        return new ArrayList<>();
    }

    /**
     * 获取pexels.com精选图片
     *
     * @param per_page 每页几张
     * @param page     第几页
     * @return
     */
    public List<String> getPexelsSelectedImages(byte per_page, byte page) {
        StringBuilder url = new StringBuilder();
        url.append("https://api.pexels.com/v1/curated?page=").append(page).append("&per_page=").append(per_page);
        String header = "563492ad6f91700001000001780c05ba42a74f438fd49998385075ca";
        return pexelsParser(url.toString(), header);
    }

    /**
     * 获取pexels.com搜索图片
     *
     * @return
     */
    public static List<String> getPexelsSearchImages(byte per_page, byte page, String keyword) {
        StringBuilder url = new StringBuilder();
        url.append("https://api.pexels.com/v1/search?query=").append(keyword).append("&locale=").append("zh-CN")
                .append("&per_page=").append(per_page).append("&page=").append(page);
        String header = "563492ad6f91700001000001780c05ba42a74f438fd49998385075ca";
        return pexelsParser(url.toString(), header);
    }

    /**
     * 获取 https://unsplash.com/ 的随机图片
     *
     * @return
     */
    public static List<String> getUnsplashRandomImages(byte num, String keyword) {
        StringBuilder url = new StringBuilder();
        String header = "A-uKl-G_c5niesOzRJQ943iNDNS5jfsChIwe-H2SNlI";
        url.append("https://api.unsplash.com").append("/photos/random").append("?query=").append(keyword)
                .append("&count=").append(num);
        String s = HttpUtil.doGet(url.toString(), new HashMap<>() {{
            put("Authorization:", header);
            put("Accept-Version:", "v1");
        }});
        System.out.println(s);
        return null;
    }

    /**
     * 获取 https://unsplash.com/分类随机图片
     *
     * @return
     */
    public static List<String> getUnsplashSearchImages(byte per_page, byte page, String keyword) {
        StringBuilder url = new StringBuilder();
        String header = "A-uKl-G_c5niesOzRJQ943iNDNS5jfsChIwe-H2SNlI";
        url.append("https://api.unsplash.com").append("/photos/random").append("?query=").append(keyword)
                .append("&count=").append(page);
        String result = HttpUtil.doGet(url.toString(), new HashMap<>() {{
            put("Authorization", header);
            put("Accept-Version", "v1");
        }});
        System.out.println(result);
        return null;
    }

    /**
     * 奇虎360壁纸接口
     * @param num
     * @param type
     * @return
     */
    public static List<String> getQiHu360Image(int num,int type,int start){
        StringBuilder url = new StringBuilder();
        url.append("http://wallpaper.apc.360.cn/index.php?c=WallPaperAndroid&a=getAppsByCategory&cid=").append(type)
                .append("&count=").append(num).append("&start=").append(start);
        String result = HttpUtil.doGet(url.toString(), null);
        JSONArray jsonArray = JSONArray.parseArray(String.valueOf(JSON.parseObject(result).get("data")));
        List<String> imageUrls = new ArrayList<>();
        for (Object ob:jsonArray){
            JSONObject jsonObject = JSON.parseObject(String.valueOf(ob));
            imageUrls.add(jsonObject.get("url").toString());
        }
        return imageUrls;
    }


    /**
     * 获取splitshire.com图片  摄影
     *
     * @return
     */
    public static List<String> getSplitshireImages() {
        String url = "https://www.splitshire.com/";
        return ImagesCrawler.splitshireHtmlAnalysis(url);
    }

    /**
     * 获取www.lifeofpix.com图片  摄影
     *
     * @return
     */
    public static List<String> getLifeofpixImages() {
        String url = "https://www.lifeofpix.com/";
        return ImagesCrawler.lifeofpixHtmlAnalysis(url);
    }

    /**
     * pexels.com 数据解析器
     *
     * @param url    网站url
     * @param header 请求头信息
     * @return
     */
    public static List<String> pexelsParser(String url, String header) {
        String str = HttpUtil.doGet(url, new HashMap<>() {{
            put("Authorization", header);
        }});
        JSONObject jsonObject = JSONObject.parseObject(str);
        JSONArray jsonArray = JSONArray.parseArray(jsonObject.get("photos").toString());
        List<String> images = new ArrayList<>();
        for (Object obj : jsonArray) {
            JSONObject urlObj = JSONObject.parseObject(JSONObject.parseObject(String.valueOf(obj)).get("src").toString());
            String imageUrl = urlObj.get("landscape").toString();
            images.add(imageUrl);
        }
        return images;
    }
}
